import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * @author
 */
public class AsyncRun07 {


    public static void main(String[] args) throws Exception {
        long start=System.currentTimeMillis();

        ExecutorService service = Executors.newCachedThreadPool();
        SumThread sumThread = new SumThread();
        FutureTask<Integer> futureTask = new FutureTask<>(sumThread);
        service.submit(futureTask);
        service.shutdown();
        //这是得到的返回值
        int result = futureTask.get();
        // 确保  拿到result 并输出
        System.out.println("异步计算结果为："+result);
        System.out.println("使用时间："+ (System.currentTimeMillis()-start) + " ms");
    }

    private static int sum() {
        return fibo(36);
    }

    private static int fibo(int a) {
        if ( a < 2) {
            return 1;
        }
        return fibo(a-1) + fibo(a-2);
    }

    static class SumThread implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            return sum();
        }
    }
}
